<?php

namespace App\Models;

class CartGoodsModel
{
    /**
     * 获取订单商品
     * entry CartGoodsExample
     * return array
     * */
    public static function getOrderCartGoodsInside($conn,$CartGoodsExample){
        $sql="SELECT `id`, `gid`, `name`, `number`, `price`, `total`, `integral`,`time`,`status`,`sku_id`,`type` FROM `cart_goods` WHERE `cid` = $CartGoodsExample->cid and `uid` = $CartGoodsExample->uid and `status` = 2 ";
        return $conn->readConn($sql, true);
    }
    /**
     * 获取订单商品(无状态)
     * entry CartGoodsExample
     * return array
     * */
    public static function getOrderCartGoodsInsideCid($conn,$CartGoodsExample){
        $sql="SELECT `id`, `gid`, `name`, `number`, `price`, `total`, `integral`,`time`,`status`,`sku_id`,`type` FROM `cart_goods` WHERE `cid` = $CartGoodsExample->cid and `uid` = $CartGoodsExample->uid";
        return $conn->readConn($sql, true);
    }
    /**
     * 获取订单一条商品名称
     * entry CartGoodsExample
     * return array
     * */
    public static function getOrderCartGoodsOneNameInsideCid($conn,$CartGoodsExample){
        $sql="SELECT `name` FROM `cart_goods` WHERE `cid` = $CartGoodsExample->cid and `uid` = $CartGoodsExample->uid limit 1";
        return $conn->readConn($sql)["name"];
    }
    /**
     * 获取订单一条商品id
     * entry CartGoodsExample
     * return array
     * */
    public static function getOrderCartGoodsOneGidInsideCid($conn,$CartGoodsExample){
        $sql="SELECT `gid`,`number` FROM `cart_goods` WHERE `cid` = $CartGoodsExample->cid and `uid` = $CartGoodsExample->uid";
        return $conn->readConn($sql,true);
    }
    /**
     * 获取订单一条数量
     * entry CartGoodsExample
     * return array
     * */
    public static function getOrderCartGoodsOneNumberInsideCid($conn,$CartGoodsExample){
        $sql="SELECT sum(`number`) as number FROM `cart_goods` WHERE `cid` = $CartGoodsExample->cid and `uid` = $CartGoodsExample->uid";
        return $conn->readConn($sql)["number"];
    }
    /**
     * 获取购物车商品
     * entry CartGoodsExample
     * return array
     * */
    public static function getCartGoodsInside($conn,$CartGoodsExample){
        $sql="SELECT `id`, `gid`, `name`, `number`, `price`, `total`, `integral`,`time`,`status`,`sku_id`,`type` FROM `cart_goods` WHERE `uid` = $CartGoodsExample->uid and `status` = 1 order by `id` desc";
        return $conn->readConn($sql, true);
    }
    /**
     * 通过id获取购物车单商品
     * entry CartGoodsExample
     * return array
     * */
    public static function getCartGoodsInsideId($conn,$CartGoodsExample){
        $sql="SELECT `id`, `gid`, `name`, `number`, `price`, `total`, `integral`,`time`,`status`,`sku_id`,`type` FROM `cart_goods` WHERE `id` = $CartGoodsExample->id and `uid` =$CartGoodsExample->uid and `status` = 1 order by `type` asc";
        return $conn->readConn($sql);
    }
    /**
     * 通过id获取购物车单总价(in)
     * entry CartGoodsExample
     * return array
     * */
    public static function getCartGoodsTotalInsideIds($conn,$CartGoodsExample){
        $sql="SELECT sum(`total`) as total, sum(`integral`) as integral FROM `cart_goods` WHERE `id` in($CartGoodsExample->id) and `uid` =$CartGoodsExample->uid and `status` = 1 and `type` = $CartGoodsExample->type";
        return $conn->readConn($sql);
    }
    public static function getCartGoodsTotalInsideIdsCount($conn,$CartGoodsExample){
        $sql="SELECT count(id) as count FROM `cart_goods` WHERE `id` in($CartGoodsExample->id) and `uid` =$CartGoodsExample->uid and `status` = 1 and `type` = $CartGoodsExample->type";
        return $conn->readConn($sql);
    }
    public static function getCartGoodsInsideIdsCount($conn,$CartGoodsExample){
        $sql="SELECT count(id) as count FROM `cart_goods` WHERE `id` in($CartGoodsExample->id) and `uid` =$CartGoodsExample->uid and `status` = 1";
        return $conn->readConn($sql);
    }
    /**
     * 购物车添加商品
     * entry CartGoodsExample
     * return int
     * */
    public static function insertCartGoods($conn,$CartGoodsExample){
        $insertSql ="INSERT INTO `cart_goods`(`uid`,`gid`,`name`,`number`,`price`,`total`,`integral`,`sku_id`,`type`) VALUES ($CartGoodsExample->uid,$CartGoodsExample->gid,'$CartGoodsExample->name','$CartGoodsExample->number',$CartGoodsExample->price,$CartGoodsExample->total,$CartGoodsExample->integral,'$CartGoodsExample->sku_id','$CartGoodsExample->type')";
        return $conn->executeConn($insertSql);
    }
    /**
     * 修改购物车商品
     * entry CartGoodsExample
     * return int
     * */
    public static function editCartGoodsInside($conn,$CartGoodsExample){
        $joinSql="";
        if($CartGoodsExample->number !=null){
            $joinSql.=" `number` = '$CartGoodsExample->number',";
        }
        if($CartGoodsExample->price !=null){
            $joinSql.=" `price` = '$CartGoodsExample->price',";
        }
        if($CartGoodsExample->total !=null){
            $joinSql.=" `total` = '$CartGoodsExample->total',";
        }
        if($CartGoodsExample->integral !=null){
            $joinSql.=" `integral` = '$CartGoodsExample->integral',";
        }
        if($CartGoodsExample->status !=null){
            $joinSql.=" `status` = '$CartGoodsExample->status',";
        }
        if($CartGoodsExample->cid !=null){
            $joinSql.=" `cid` = '$CartGoodsExample->cid',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `cart_goods` SET $innerJoin WHERE `id` in($CartGoodsExample->id) and `uid` = $CartGoodsExample->uid";
            return $conn->executeConn($upUserSql);
        }
    }
}